import random

import pyautogui

from find import Tree, Get, Notify, Food, Eat, FoodFull, ErrNumber, Head
import operate
from run import TAB_WIN

tree = Tree()
food = Food()
food_full = FoodFull()
get = Get()
notify = Notify()
eat_pane = Eat()
err_number = ErrNumber()
head = Head()


def merge_food(point):
    """
    寻找food, 并尝试将第二个food合并入第一个food
    """
    try:
        operate.move_left(food.point())
        operate.next_tick()
        operate.move_left(point)
    except pyautogui.ImageNotFoundException:
        print('none food2, pass')


def check_tree_unchoosed():
    try:
        _ = head.match()
        return False
    except pyautogui.ImageNotFoundException:
        return True


def eat_food():
    """
    执行喂树动作
    """
    # 选中
    try:
        operate.reset()
        operate.next_tick()
        operate.press('z')
        operate.next_tick()

        if check_tree_unchoosed():
            # 未锁中树, 重新开树
            return 1

        # 点喂
        eat_btn = eat_pane.offset((255, 370), 10, 12)
        operate.move_left(eat_btn)
        operate.next_tick()
        # 喂一次
        ff = food_full.point()
        operate.shift_click(ff)
        operate.next_tick()
        operate.only_right()
        operate.next_tick()
        merge_food(ff)
        operate.next_tick()
    except pyautogui.ImageNotFoundException:
        print('none food, pass')

    return 3


def start_tree():
    """
    开树
    """
    try:
        operate.reset()
        operate.press('b')
        operate.next_tick()
        operate.press('b')
        operate.next_tick()
        operate.move_right(tree.point())
        operate.next_tick()
    except pyautogui.ImageNotFoundException:
        print('none tree, pass')
        
        # 若循环窗口检测
        if TAB_WIN:
            operate.tab_win()

        return 3

    return 2


def fixEatError():
    """修正喂树时弹出数字键盘"""
    try:
        errClose = err_number.offset((125, 20), 10, 12)
        operate.move_left(errClose)
        return True
    except pyautogui.ImageNotFoundException:
        print('un error, pass')
        return False


def get_treed(need_check_tree=False):
    """
    领豆豆
    """
    try:
        # 间隔5~20s检测一次
        pyautogui.sleep(random.randint(5, 20))

        error = need_check_tree and check_tree_unchoosed()
        error = error or fixEatError()
        if error:
            return 1

        getable = notify.point()

        operate.move_left(getable)
        operate.next_tick()
        operate.move_left(get.point())
        operate.next_tick()

        return 1
    except pyautogui.ImageNotFoundException:
        if not need_check_tree:
            return get_treed(True)

        # 若循环窗口检测
        if TAB_WIN:
            operate.tab_win()

        # 重新执行步骤3
        return 3


def get_tree(point):
    for i in range(40):
        operate.press('z')
        pyautogui.sleep(0.2)
        operate.move_left(point)
        pyautogui.sleep(0.3)
        operate.press('enter')
        pyautogui.sleep(0.2)


def execute(start_step=3):
    step = start_step
    while True:
        # 第一步, 开树
        if step == 1:
            step = start_tree()
        # 第二步, 喂树
        elif step == 2:
            step = eat_food()
        # 第三步, 领树
        elif step == 3:
            step = get_treed()
